// miniprogram/pages/admin/editImage.js
const regeneratorRuntime = require('../../libs/runtime')
Page({
    data: {
        img: [],
        collapseName: ""
    },
    async onLoad(options) {
        wx.showLoading({
            title: '加载中',
        })
        await this.getImage()
        wx.hideLoading()
    },
    async getImage() {
        const db = wx.cloud.database()
        let {
            data
        } = await db.collection('collapse_img').doc('ec83e37a-0e1c-44f6-87f7-f409e64aa8fe').get()
        this.setData({
            img: data.img,
            collapseName: data.collapseName
        })
    },
    myInput(e) {
        let name = e.currentTarget.dataset.name;
        let value = e.detail.detail.value
        this.setData({
            [name]: value
        })
    },
    chooseImage(e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    mask: true
                })
                wx.cloud.uploadFile({
                    cloudPath: '' + Date.parse(new Date()), // 上传至云端的路径
                    filePath: res.tempFilePaths[0], // 小程序临时文件路径
                    success: res => {
                        // 返回文件 ID
                        that.setData({
                            img: that.data.img.concat(res.fileID)
                        })
                        wx.hideLoading()
                        wx.showToast({
                            title: '上传成功',
                        })
                    },
                    fail: error => {
                        wx.showToast({
                            title: error,
                        })
                    }
                })
            },
            fail: function(err) {
                console.log(err)
            }
        })
    },
    deleteImage(e) {
        let id = e.currentTarget.id
        let that = this;
        let tmp = that.data.img
        let index = tmp.indexOf(id);
        wx.showModal({
            title: '提示',
            content: '确认删除这张图片么',
            showCancel: true,
            success: function(res) {
                if (res.confirm) {
                    tmp.splice(index, 1);
                    that.setData({
                        img: tmp
                    })
                } else {

                }
            },
        })
    },
    save() {
        wx.showLoading({
            title: '保存中',
        })
        wx.cloud.callFunction({
            name: 'editImage',
            data: this.data,
            success: res => {
                wx.showToast({
                    title: '保存成功',
                })
            },
            fail: err => {
                wx.showToast({
                    icon: "none",
                    title: '保存失败，请重试',
                })
                console.log('[云函数] [addRoom] 失败，请检查是否有部署云函数，错误信息：', err)
            },
            complete() {
                wx.hideLoading()
            }
        })
    }
})